package com.haobangs.imms.service.impl;

import java.util.Arrays;
import java.util.List;
import com.ruoyi.common.config.MyLambdaQueryWrapper;
import com.ruoyi.common.utils.DateUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.haobangs.imms.mapper.AlarmRecordMapper;
import com.haobangs.imms.domain.AlarmRecord;
import com.haobangs.imms.service.IAlarmRecordService;

/**
 * 报警记录Service业务层处理
 *
 * @author haobangss
 * @date 2023-06-29
 */
@Service
public class AlarmRecordServiceImpl implements IAlarmRecordService
{
    @Autowired
    private AlarmRecordMapper alarmRecordMapper;

    /**
     * 查询报警记录
     *
     * @param id 报警记录主键
     * @return 报警记录
     */
    @Override
    public AlarmRecord selectAlarmRecordById(Long id)
    {
        return alarmRecordMapper.selectById(id);
    }

    /**
     * 分页查询报警记录
     *
     * @param alarmRecord 报警记录
     * @return Page 对象
     */
    public Page getByPage(AlarmRecord alarmRecord){
        MyLambdaQueryWrapper<AlarmRecord> wrapper = new MyLambdaQueryWrapper<>();
                    wrapper.eqIfPresent(AlarmRecord::getFactoryId, alarmRecord.getFactoryId());
                    wrapper.eqIfPresent(AlarmRecord::getWorkshopId, alarmRecord.getWorkshopId());
                    wrapper.eqIfPresent(AlarmRecord::getEdgeId, alarmRecord.getEdgeId());
                    wrapper.likeIfPresent(AlarmRecord::getDeviceName, alarmRecord.getDeviceName());
                    wrapper.eqIfPresent(AlarmRecord::getDeviceSerialNumber, alarmRecord.getDeviceSerialNumber());
                    wrapper.eqIfPresent(AlarmRecord::getEdgeTime, alarmRecord.getEdgeTime()).orderByDesc(AlarmRecord::getId);
        Page<AlarmRecord> alarmRecords = alarmRecordMapper.selectPage(new Page<>(alarmRecord.getPageNum(), alarmRecord.getPageSize()), wrapper);
        return alarmRecords;
    }

    /**
     * 查询报警记录列表
     *
     * @param alarmRecord 报警记录
     * @return 报警记录
     */
    @Override
    public List<AlarmRecord> selectAlarmRecordList(AlarmRecord alarmRecord)
    {
        MyLambdaQueryWrapper<AlarmRecord> wrapper = new MyLambdaQueryWrapper<>();
                    wrapper.eqIfPresent(AlarmRecord::getFactoryId, alarmRecord.getFactoryId());
                    wrapper.eqIfPresent(AlarmRecord::getWorkshopId, alarmRecord.getWorkshopId());
                    wrapper.eqIfPresent(AlarmRecord::getEdgeId, alarmRecord.getEdgeId());
                    wrapper.likeIfPresent(AlarmRecord::getDeviceName, alarmRecord.getDeviceName());
                    wrapper.eqIfPresent(AlarmRecord::getDeviceSerialNumber, alarmRecord.getDeviceSerialNumber());
                    wrapper.eqIfPresent(AlarmRecord::getEdgeTime, alarmRecord.getEdgeTime());
        return alarmRecordMapper.selectList(wrapper);
    }

    /**
     * 新增报警记录
     *
     * @param alarmRecord 报警记录
     * @return 结果
     */
    @Override
    public int insertAlarmRecord(AlarmRecord alarmRecord)
    {
        alarmRecord.setCreateTime(DateUtils.getNowDate());
        return alarmRecordMapper.insert(alarmRecord);
    }

    /**
     * 修改报警记录
     *
     * @param alarmRecord 报警记录
     * @return 结果
     */
    @Override
    public int updateAlarmRecord(AlarmRecord alarmRecord)
    {
        alarmRecord.setUpdateTime(DateUtils.getNowDate());
        return alarmRecordMapper.updateById(alarmRecord);
    }

    /**
     * 批量删除报警记录
     *
     * @param ids 需要删除的报警记录主键
     * @return 结果
     */
    @Override
    public int deleteAlarmRecordByIds(Long[] ids)
    {
        if(ids.length == 0){
            throw new RuntimeException("id不能为空");
        }
        return alarmRecordMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除报警记录信息
     *
     * @param id 报警记录主键
     * @return 结果
     */
    @Override
    public int deleteAlarmRecordById(Long id)
    {
        return alarmRecordMapper.deleteById(id);
    }
}
